home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Authority.sea / XML Authority / Required / importers / ImportLDAPMetaSample.java < prev    next >
Encoding:
Java Source  |  2000-05-03  |  4.7 KB  |  160 lines  |  [TEXT/CWIE]

  1. /*
  2.  
  3.     ImportLDAPMeta
  4.     Created 4/27/99
  5.     Copyright 1999 Extensibility. All rights reserved.
  6.  
  7. */
  8.  
  9. import com.extensibility.convert.MetaSpectIntf;
  10. import java.util.Vector;
  11. import java.util.Hashtable;
  12. import java.util.Enumeration;
  13. import netscape.ldap.*;
  14.  
  15. public class ImportLDAPMetaSample implements MetaSpectIntf {
  16.  
  17.     public static final String OID = "OID";  // value String
  18.     String sourceString = "LDAPV3"; // The name of the importer itself, shows up in the XA menu
  19.     String promptString = "Enter the hostname, userid and password for the LDAP source.";
  20.     String listPrompt = "Select objectclasses to import."; 
  21.     Integer ldapport = new Integer(389);
  22.     LDAPSchema dirSchema = new LDAPSchema();
  23.  
  24.     LDAPConnection lc;
  25.  
  26.     /** The way we keep track of objectclasses (tables) */
  27.     class TableSpec implements MetaSpectIntf.TableIntf {
  28.         String objectclass;
  29.  
  30.         public TableSpec(String objectclass){
  31.             this.objectclass = objectclass;
  32.         }
  33.  
  34.         /** Just the table name. */
  35.         public String getShortName(){
  36.             return objectclass;
  37.         }
  38.  
  39.         public String getLongName(){
  40.             return objectclass;
  41.         }
  42.  
  43.         public String toString(){
  44.             return getLongName();
  45.         }
  46.     }
  47.  
  48.     /* @see MetaSpectIntf.getPrompt(String) */
  49.     public String getPrompt(int which) {
  50.         switch (which){
  51.             case PROMPT_IMPORTER:
  52.                 return sourceString;
  53.             case PROMPT_SOURCE:
  54.                 return promptString;
  55.             case PROMPT_LIST:
  56.                 return listPrompt;
  57.             case PROMPT_REFINE:
  58.                 return null;
  59.         }
  60.         return "";
  61.     }
  62.  
  63.    /**
  64.     Opens a connection to an LDAP server, queries the schema and then
  65.         closes the connection.  Requires a connectstring that can either
  66.         be equivalent to the hostname or the hostname and port:
  67.           ldap.extensibility.com or ldap.extensibility.com:389
  68.         The user string MUST be a distinguished name (DN) such as:
  69.           cn=Directory Manager or uid=smith,o=Extensibility,c=US
  70.     */
  71.     public void open(String connectString, String user, String password) throws LDAPException{
  72.         if (connectString.indexOf(":") >= 0) {
  73.             ldapport = new Integer(connectString.substring(connectString.indexOf(":")+1));
  74.             connectString = connectString.substring(0,connectString.indexOf(":"));
  75.         }
  76.         // Connect to LDAP server and acquire schema.
  77.         LDAPConnection lc = new LDAPConnection();
  78.         lc.connect(connectString,ldapport.intValue());
  79.         if ((user != null)&&(password != null))
  80.             lc.authenticate(user,password);
  81.         dirSchema.fetchSchema( lc );
  82.     }
  83.  
  84.     /**
  85.         Return a list of objectclasses available.
  86.         @see MetaSpectIntf.getChoiceList()
  87.     */
  88.     public Enumeration getChoiceList() {
  89.             Vector myChoice = new Vector();
  90.         Enumeration oc = dirSchema.getObjectClassNames();
  91.             while (oc.hasMoreElements())
  92.             myChoice.addElement(new TableSpec((String)oc.nextElement()));
  93.             return myChoice.elements();
  94.     }
  95.  
  96.       /**
  97.         Return a particular objectclass.
  98.         @see MetaSpectIntf.getMetaInfo(MetaSpectIntf.TableIntf)
  99.     */
  100.     public Enumeration getMetaInfo(MetaSpectIntf.TableIntf choice) {
  101.             TableSpec tableInfo = (TableSpec) choice;
  102.             LDAPObjectClassSchema myObject = dirSchema.getObjectClass(tableInfo.getShortName());
  103.             Vector Attributes = new Vector();
  104.             Enumeration ra = myObject.getRequiredAttributes();
  105.             while (ra.hasMoreElements())
  106.               Attributes.addElement( this.getOneColumn((String)ra.nextElement(), true));
  107.             Enumeration oa = myObject.getOptionalAttributes();
  108.             while (oa.hasMoreElements())
  109.                   Attributes.addElement( this.getOneColumn((String)oa.nextElement(), false));
  110.             return Attributes.elements();
  111.     }
  112.  
  113.       /**
  114.         Return a particular attribute.
  115.     */
  116.       protected Hashtable getOneColumn(String columnName, boolean isRequired){
  117.         Hashtable myColumn = new Hashtable(){
  118.               // we override to catch null values
  119.               public Object put(Object key, Object value){
  120.                 if (value == null)
  121.                     return null;
  122.                   return super.put(key, value);
  123.                   }
  124.         };
  125.             LDAPAttributeSchema attribute = dirSchema.getAttribute(columnName);
  126.         myColumn.put(NAME, attribute.getName() );
  127.         myColumn.put(DATATYPE, new Integer(attribute.getSyntax()));
  128.         myColumn.put(NATIVEDATATYPE, attribute.getValue() );
  129.         myColumn.put(REMARKS, attribute.getDescription() );
  130.         if (!attribute.isSingleValued()) {
  131.               myColumn.put(IS_ARRAY, Boolean.TRUE);
  132.         }
  133.         myColumn.put(OID, attribute.getOID() );
  134.         myColumn.put(ISNULLABLE, new Boolean(!isRequired));
  135.         myColumn.put(IS_ID, Boolean.FALSE);
  136.         return myColumn;
  137.     }
  138.  
  139.  
  140.       // Stub for MetaSpectIntf inteface.
  141.       public java.util.Enumeration getRefineList(MetaSpectIntf.TableIntf myTable) {
  142.         return null;
  143.     }
  144.  
  145.       // Stub for MetaSpectIntf implementation.  No function since the
  146.       //  connection is closed immediately after retrieving schema information.
  147.       //  @open, above.
  148.     public void close() {
  149.         if (lc == null)
  150.             return;
  151.         try {
  152.             lc.disconnect();
  153.         }
  154.         catch (LDAPException f) {
  155.             // do nothing
  156.         }
  157.  
  158.     }
  159.  
  160. }